<?php

if ( !function_exists('sys_get_temp_dir')) {
  	function sys_get_temp_dir() {
      if( $temp=getenv('TMP') )        return $temp;
      if( $temp=getenv('TEMP') )        return $temp;
      if( $temp=getenv('TMPDIR') )    return $temp;
      $temp=tempnam(__FILE__,'');
      if (file_exists($temp)) {
          unlink($temp);
          return dirname($temp);
      }
      return null;
  	}
 	}

class HelpdeskTicket
{
	private $ch; // curl handle
	private $cookiefile;
	private $firstName = 'classroom';
	private $email = 'noreply@edison.edu';
	private $loginPostPage = 'http://d2.parature.com/ics/support/mylogin.asp';
	private $ticketPostPage = 'http://d2.parature.com/ics/support/ticketNewProcess.asp';
	
	function __construct()
	{
		// set cookie file, init curl
		$this->cookiefile = tempnam(sys_get_temp_dir(),"TICKET_COOKIE");
		$this->ch = curl_init();
		
		curl_setopt($this->ch,CURLOPT_AUTOREFERER,TRUE); // automatically update referrer when following Location redirects
		curl_setopt($this->ch,CURLOPT_FOLLOWLOCATION,TRUE); // Follow redirects in case the server relies on the behaviour
		curl_setopt ($this->ch, CURLOPT_COOKIEJAR, $this->cookiefile); // set cookie file 
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); // We don't want to output the response
	}
	
	function __destruct()
	{
		// release curl handle
		if($this->ch)
		{
			curl_close($this->ch);
		}
		// delete cookie file
		@unlink($this->cookiefile); // file may not exist, so supress the error
	}
	
	private function GetURL($url)
	{
		// setup options
		curl_setopt($this->ch, CURLOPT_URL, $url);
		curl_setopt($this->ch, CURLOPT_REFERER, $url);
		curl_setopt($this->ch, CURLOPT_POST, FALSE);
		curl_setopt($this->ch, CURLOPT_POSTFIELDS, '');
		// execute and return response
		return curl_exec($this->ch);
	}
	
	private function PostToURL($url,$postvars)
	{
		// setup url and referrer
		curl_setopt($this->ch, CURLOPT_URL, $url);
		curl_setopt($this->ch, CURLOPT_REFERER, $url);
		// set post vars
		curl_setopt ($this->ch, CURLOPT_POSTFIELDS, $postvars);
		curl_setopt ($this->ch, CURLOPT_POST, 1);
		// execute and return response
		return curl_exec($this->ch);
	}
	
	// Login and get a session cookie
	private function login()
	{	
			$this->firstName = 'Classroom';
			$this->email = 'classroomdown@edison.edu';
		

		// Get session cookie by visiting this page, check if it succeeded
		if(!$this->GetURL('http://d2.parature.com/ics/support/default.asp?deptID=4076'))
		{
			return false;
		}
		// Sign in
		$post = "task=login2&firstName=" . urlencode($this->firstName) . "&email=" . urlencode($this->email);
		return $this->postToURL($this->loginPostPage,$post); 
	}
	
	// Returns true on success, false on error
	public function SendClassroomDown($description, $room)
	{
		// login to the ticket system
		if(!$this->login())
		{
			return false;
		}
		
		// create post variable
		$description = urlencode($description);
		$room = urlencode($room);
		$email = urlencode('classroomdown@edison.edu');
		$postvars ="isfinished=true&customerID=5822004&additionalContactID=&" . 
		"FIELD_76329_DROPDOWN_null=151392&".
		"FIELD_123128_DROPDOWN_null=261457&".
		"FIELD_76330_DROPDOWN_null=151399&".
		"FIELD_76340_DROPDOWN_null=151460&".
		"FIELD_76116_TEXTAREA_varchar=" . $description .
		"&FIELD_76518_TEXT_varchar=" . $room .
		"&FIELD_84955_DROPDOWN_null=242583&notification=1&wizard_step=onestep&is_last_step=1";
		
		// post to the ticket input page url		
		$response = $this->PostToURL($this->ticketPostPage,$postvars);
		//$response = 'Confirmation';
		
		// did the post succeed?
		if(!$response)
		{			
			return false;
		}
		
		// check for 'Confirmation' in the response, return true if found, false if not
		return strstr($response,'Confirmation') ? true : false;
	}
	
	public function SignInTicket($name, $reason){
		echo "";
	}
	
	public function getLastErrorString()
	{
		return curl_error($this->ch);
	}
	
	
}

?>